﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Ports;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using nStruct;
using nUtil;

namespace nFile
{

	public static class cFile
	{
        public static void Config_File_Write(string inFileName, Com_Conf_Struct inDat)
        {
            
			StreamWriter fw =null;

			fw = new StreamWriter(inFileName);
			string str = "";
			
			str = "LDS2000 Configure File";
			fw.WriteLine(str);

			fw.WriteLine("PORT Name 1" + "\t" + inDat.PortName[0]);
			fw.WriteLine("PORT Name 2" + "\t" + inDat.PortName[1]);
			fw.WriteLine("PORT Name 3" + "\t" + inDat.PortName[2]);

            fw.WriteLine("Conf. FileName 1" + "\t" + inDat.PS_Conf_FileName[0]);
            fw.WriteLine("Conf. FileName 2" + "\t" + inDat.PS_Conf_FileName[1]);
			fw.WriteLine("Conf. FileName 3" + "\t" + inDat.PS_Conf_FileName[2]);

            fw.Close();
       }


        public static Com_Conf_Struct Config_File_Read(string inFileName)
        {

            Com_Conf_Struct ret = new Com_Conf_Struct();
            StreamReader fr = null;

            fr = new StreamReader(inFileName);
            string str = "";
            
            str = fr.ReadLine();

            if (str != "LDS2000 Configure File")
            {

                if (fr != null) fr.Close();
                MessageBox.Show("File Format Error", "Error Message");
                return ret;
            }

            for(int i=0;i<3;i++) ret.PortName[i] = cUtil.String_Convert_to_mString(1, '\t', fr.ReadLine());
			for (int i = 0; i < 3; i++) ret.PS_Conf_FileName[i] = cUtil.String_Convert_to_mString(1, '\t', fr.ReadLine());

			fr.Close();
			return ret;
        }

		public static void Log_Data_Append(PS_Conf_Struct inDat)
		{
			string str = "";
			string inFile = "";
			inFile = inDat.LogFolder+inDat.LogFileName;
			//inFile = inDat.LogFileName;
			StreamWriter fw = File.AppendText(inFile);

			DateTime dt = DateTime.Now;
			str = String.Format("{0:D4}.{1:D2}.{2:D2} {3:D2}:{4:D2}:{5:D2}",dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second);


			for (int i = 0; i < inDat.HW_No; i++)
			{
				str += "\t" + inDat.HW_I_Value[i];
			}

			for (int i = 0; i < inDat.TempNo; i++)
			{
				str += "\t" + String.Format("{0:0.0}", inDat.Temp_Value[i]);
			}
			fw.WriteLine(str);
			fw.Close();
		}

		public static void Log_Data_Making_Header(PS_Conf_Struct inDat)
		{
			StreamWriter fw =null;

			fw = new StreamWriter(inDat.LogFolder+inDat.LogFileName);
			string str = "";
			
			str = "PS and Temp Log Data File";
			fw.WriteLine(str);

			str = "PS and Temp Name";
			for (int i = 0; i < inDat.HW_No; i++)
			{
				str += "\t" + inDat.HW_PsName[i];

			}
			for (int i = 0; i < inDat.TempNo; i++)
			{
				str += "\t" + inDat.TempName[i];
			}

			fw.WriteLine(str);

			str = "V Set(V)";
			for (int i = 0; i < inDat.PsNo; i++)
			{
				for (int j = 0; j < inDat.StringNo[i]; j++)
				{
					str += "\t" + String.Format("{0:0.0}", inDat.Vset[i]);
				}
			}

			//blank 처리
			for (int i = 0; i < inDat.TempNo; i++)
			{
				str += "\t";
			}
			fw.WriteLine(str);

			str = "I Set(mA)";
			for (int i = 0; i < inDat.PsNo; i++)
			{
				for (int j = 0; j < inDat.StringNo[i]; j++)
				{
					str += "\t" + String.Format("{0:0.0}", inDat.Iset[i]);
				}
			}
			
			//blank 처리
			for (int i = 0; i < inDat.TempNo; i++)
			{
				str += "\t";
			}
			fw.WriteLine(str);


			str = "I(mA) and Temp(oC) Protect";
			for (int i = 0; i < inDat.PsNo; i++)
			{
				for (int j = 0; j < inDat.StringNo[i]; j++)
				{
					str += "\t" + String.Format("{0:0.0}", inDat.IProtect[i]);
				}
			}
			for (int i = 0; i < inDat.TempNo; i++)
			{
				str += "\t" + String.Format("{0:0.0}", inDat.TempProtect[i]);
			}
			fw.WriteLine(str);

			fw.Close();
		}

		public static string[] Log_File_Header_Find(int key,string inFileName)
		{
			string strTitle="";
			if (key == 0) strTitle = "LDS1000_PS_LogData_File";//PS Header
			else strTitle = "LDS1000_Temp_LogData_File";//temp Header
			StreamReader fr = null;
			string str;
			string[] ret = new string[100];
			try
			{
				fr = new StreamReader(inFileName);
				if (fr.ReadLine() == strTitle)
				{
					str = fr.ReadLine();
					ret = cUtil.char_split('\t', str);
				}
				else
				{
					ret[0] = "";
				}

				fr.Close();
				return ret;
			}
			catch
			{
				ret[0] = "";
				return ret;
			}
		}

		
		public static void PowerSetup_FileSave(PS_Conf_Struct inDat)
		{
			StreamWriter fw = null;
			string str="";
			try
			{
				fw = new StreamWriter(inDat.FullFileName);
				fw.WriteLine("LDS1000_Configure_File");

				fw.WriteLine("PS No ," + inDat.PsNo.ToString());
				fw.WriteLine("V Set Master ," + inDat.VsetMaster.ToString());
				fw.WriteLine("I Set Master ," + inDat.IsetMaster.ToString());
				fw.WriteLine("I Protect Master ," + inDat.IProtect_Master.ToString());
				for (int i = 0; i < inDat.PsNo; i++)
				{
					str = "PS " + (i + 1).ToString() + " ," + inDat.PsName[i] + ",";
					str += inDat.PsEn[i].ToString() + ",";
					str += inDat.StringNo[i].ToString() + ",";
					str += inDat.Vset[i].ToString() + ",";
					str += inDat.Iset[i].ToString() + ",";
					str += inDat.IProtect[i].ToString();
					fw.WriteLine(str);
				}

				fw.WriteLine("Temp No ," + inDat.TempNo.ToString());
				fw.WriteLine("Temp Protect Master ," + inDat.TempProtectMaster.ToString());
				for (int i = 0; i < inDat.TempNo; i++)
				{
					str = "Temp " + (i + 1).ToString() + " ," + inDat.TempName[i] + ",";
					str += inDat.TempEn[i].ToString() + ",";
					str += inDat.TempProtect[i].ToString();
					fw.WriteLine(str);
				}
				fw.Close();
			}
			catch
			{
				if (fw != null) fw.Close();
				MessageBox.Show("File Writting Error", "Error Message");
				return;
			}

		}

		public static PS_Conf_Struct PowerSetup_FileOpen(string inFileName)
		{
			PS_Conf_Struct OutDat = new PS_Conf_Struct();
			StreamReader fr = null;
			string str;
			try
			{
				
				fr = new StreamReader(inFileName);
				
				if (fr.ReadLine() != "LDS1000_Configure_File")
				{
					fr.Close();
					MessageBox.Show("File format Error", "Error Message");
					return OutDat;
				}

				OutDat.PsNo = cUtil.String_Convert_to_mInteger(1, ',', fr.ReadLine());
				OutDat.VsetMaster = cUtil.String_Convert_to_mFloat(1, ',', fr.ReadLine());
				OutDat.IsetMaster = cUtil.String_Convert_to_mFloat(1, ',', fr.ReadLine());
				OutDat.IProtect_Master = cUtil.String_Convert_to_mFloat(1, ',', fr.ReadLine());

				for (int i = 0; i < OutDat.PsNo; i++)
				{
					str = fr.ReadLine();
					OutDat.PsName[i] = cUtil.String_Convert_to_mString(1, ',', str);
					OutDat.PsEn[i] = cUtil.String_Convert_to_mInteger(2, ',', str);
					OutDat.StringNo[i] = cUtil.String_Convert_to_mInteger(3, ',', str);
					OutDat.Vset[i] = cUtil.String_Convert_to_mFloat(4, ',', str);
					OutDat.Iset[i] = cUtil.String_Convert_to_mFloat(5, ',', str);
					OutDat.IProtect[i] = cUtil.String_Convert_to_mFloat(6, ',', str);
				}


				OutDat.TempNo = cUtil.String_Convert_to_mInteger(1, ',', fr.ReadLine());
				OutDat.TempProtectMaster = cUtil.String_Convert_to_mByte(1, ',', fr.ReadLine());

				for (int i = 0; i < OutDat.TempNo; i++)
				{
					str = fr.ReadLine();
					OutDat.TempName[i] = cUtil.String_Convert_to_mString(1, ',', str);
					OutDat.TempEn[i] = cUtil.String_Convert_to_mInteger(2, ',', str);
					OutDat.TempProtect[i] = cUtil.String_Convert_to_mByte(3, ',', str);
				}

				fr.Close();
			}
			catch
			{
				if (fr != null) fr.Close();
				MessageBox.Show("File Reading Error", "Error Message");
				return OutDat;
			}

			//OutDat.FullFileName = inFileName;
			return OutDat;
		}

	}
}